<?php
namespace Course\Controller;
use Common\Controller\AdminbaseController;
class AdminApplyController extends AdminbaseController {

	protected $order_obj;
	function _initialize() {
		parent::_initialize();

	}
	function index(){
		$where = " and 1 = 1";
		$uwhere = " and 1 = 1";
		if(IS_POST){
			$keyword = I('keyword');
			if($keyword){
				$where .= " and (user_nicename like '%$keyword%' or mobile like '%$keyword%')";
				$uwhere .= " and (username like '%$keyword%')";
			}
		}
		$apply = M('course_apply')->where("del = '0'")->order("addtime desc")->select();
		foreach($apply as $v){
			$uid = $v['uid'];
			$cid = $v['cid'];
			if($keyword){
				$users = M('users')->field('user_nicename,mobile')->where("id = '$uid'".$where)->find();
				$userinfo = M('user_show')->field('username')->where("uid = '$uid'".$uwhere)->find();
				if($users){
					$userinfo = M('user_show')->field('username')->where("uid = '$uid'")->find();
				}
				if($userinfo){
					$users = M('users')->field('user_nicename,mobile')->where("id = '$uid'")->find();
				}
			}else{
				$users = M('users')->field('user_nicename,mobile')->where("id = '$uid'")->find();
				$userinfo = M('user_show')->field('username')->where("uid = '$uid'")->find();
			}
			
			$course = M('course')->field('cs_name')->where("id = '$cid'")->find();
			$v['user_nicename'] = $users['user_nicename'];
			$v['mobile'] = $users['mobile'];
			$v['course'] = $course['cs_name'];
			$v['username'] = $userinfo['username'];
			if($users && $userinfo){
				$a[] = $v;
			}
			
		}
		$state = M('course_apply_state')->where("id = '1'")->find();
		$this->assign('state',$state);
		$this->assign('apply',$a);
		$this->display();
	}
	
	//通过
	function adopt(){
		$id = I('id');
		$data['state'] = 1;
		$save = M('course_apply')->where("id = '$id'")->save($data);
		if ($save) {
			$this->success("审核成功！",U("index"));
		} else {
			$this->error("审核失败！");
		}
	}
	
	//驳回
	function reject(){
		$id = I('id');
		$data['state'] = 2;
		$save = M('course_apply')->where("id = '$id'")->save($data);
		if ($save) {
			$this->success("审核成功！",U("index"));
		} else {
			$this->error("审核失败！");
		}
	}
	
	//驳回
	function type(){
		$id = I('id');
		$data['type'] = 1;
		$save = M('course_apply')->where("id = '$id'")->save($data);
		if ($save) {
			$this->success("课程考核成功！",U("index"));
		} else {
			$this->error("课程考核失败！");
		}
	}
	
	//删除
	function delete(){
		$id = I('id');
		$data['del'] = 1;
		$save = M('course_apply')->where("id = '$id'")->save($data);
		if ($save) {
			$this->success("删除成功！",U("index"));
		} else {
			$this->error("删除失败！");
		}
	}
	
	function open(){
		$data['state'] = 1;
		$save = M('course_apply_state')->where("id = '1'")->save($data);
		if ($save) {
			$this->success("已开启课程申请！",U("index"));
		} else {
			$this->error("开启失败！");
		}
	}

	function close(){
		$data['state'] = '0';
		$save = M('course_apply_state')->where("id = '1'")->save($data);
		if ($save) {
			$this->success("已关闭课程申请！",U("index"));
		} else {
			$this->error("关闭失败！");
		}
	}
}